//
//  IJKAudioController.m
//  IJKMediaDemo
//
//  Created by os on 10/18/24.
//  Copyright © 2024 bilibili. All rights reserved.
//

#import "IJKAudioController.h"

@interface IJKAudioController ()

@property (nonatomic,strong) IJKFFMoviePlayerController *player;

@end

@implementation IJKAudioController

// 快速创建按扭
- (void)createBtn:(NSString *)title frame:(CGRect)frame action:(SEL)action{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:frame];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor grayColor]];
    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    [IJKFFMoviePlayerController setLogReport:YES];
//    [IJKFFMoviePlayerController setLogLevel:k_IJK_LOG_DEBUG];
//    [IJKFFMoviePlayerController checkIfFFmpegVersionMatch:YES];
    //IJKFFOptions *options = [IJKFFOptions optionsByDefault];
    

    [self createBtn:@"播放 1" frame:CGRectMake(40, 40, 100, 60) action:@selector(play1)];
    [self createBtn:@"播放 2" frame:CGRectMake(40, 110, 100, 60) action:@selector(play2)];
    [self createBtn:@"暂停" frame:CGRectMake(40, 180, 100, 60) action:@selector(stop)];
    
    [self createBtn:@"开始录制" frame:CGRectMake(160, 40, 100, 60) action:@selector(record)];
    [self createBtn:@"停止录制" frame:CGRectMake(160, 110, 100, 60) action:@selector(stopRecord)];
    
    [self createBtn:@"播放录制" frame:CGRectMake(280, 40, 100, 60) action:@selector(playRecord)];
    [self createBtn:@"停止" frame:CGRectMake(280, 110, 100, 60) action:@selector(stopPlayRecord)];
    
    [self createBtn:@"Disabled" frame:CGRectMake(400, 40, 100, 60) action:@selector(eq0)];
    [self createBtn:@"Electronic" frame:CGRectMake(400, 110, 100, 60) action:@selector(eq7)];
    [self createBtn:@"Classical" frame:CGRectMake(400, 180, 100, 60) action:@selector(eq4)];

}

- (void)play1{
    
    NSURL *url = [NSURL URLWithString:@"https://stream.gmanews.tv/ioslive/livestream/playlist.m3u8"];
    [self.player stop];
    self.player = [[IJKFFMoviePlayerController alloc] initWithContentURL:url withOptions:nil];
    self.player.recognizeMusic = ^(const char *songName, const char *artistName, const char *avator, const char *id1, const char *id2) {
        NSLog(@"识别到了歌曲 [%@],[%@]\n",[NSString stringWithCString:songName encoding:NSUTF8StringEncoding],[NSString stringWithCString:artistName encoding:NSUTF8StringEncoding]);
    };
    self.player.blockType = ^(const char *type, void *data) {
        float time = *(float*)data;
        printf("录制 type = %s,data = %f\n",type, time);
    };
    self.player.shouldAutoplay = YES;
    [self.player prepareToPlay];
    [self.player play];
}

- (void)play2{
    
    NSURL *url = [NSURL URLWithString:@"https://piccpndali.v.myalicdn.com/audio/cctv13_2.m3u8"];
    [self.player stop];
    self.player = [[IJKFFMoviePlayerController alloc] initWithContentURL:url withOptions:nil];
    self.player.recognizeMusic = ^(const char *songName, const char *artistName, const char *avator, const char *id1, const char *id2) {
        NSLog(@"识别到了歌曲 [%@],[%@]\n",[NSString stringWithCString:songName encoding:NSUTF8StringEncoding],[NSString stringWithCString:artistName encoding:NSUTF8StringEncoding]);
    };
    self.player.blockType = ^(const char *type, void *data) {
        float time = *(float*)data;
        printf("录制 type = %s,data = %f\n",type, time);
    };
    self.player.shouldAutoplay = YES;
    [self.player prepareToPlay];
    [self.player play];
}

- (void)stop{
    [self.player stop];
}

- (void)record{
    
    NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"recordmusic.mp3"];
    [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
    
    [self.player startRecord:path];
}

- (void)stopRecord{
    [self.player stopRecord];
}

- (void)playRecord{
    
    NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"recordmusic.mp3"];
    NSURL *url = [NSURL fileURLWithPath:path];

    [self.player stop];
    self.player = [[IJKFFMoviePlayerController alloc] initWithContentURL:url withOptions:nil];
    self.player.shouldAutoplay = YES;

    [self.player prepareToPlay];
    [self.player play];
}

- (void)stopPlayRecord{
    [self.player stop];
}

- (void)eq0{
    [self.player setEQ:0];
}

- (void)eq4{
    [self.player setEQ:4];
}

- (void)eq7{
    [self.player setEQ:7];
}



@end
